﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using BannerLib4Net.Core.Utilities.Configuration.Exceptions;

namespace BannerLib4Net.Core.Utilities.Configuration
{
    public class SettingsUtility
    {
        public SettingsUtility()
        {
        }

        public static object ReadValue(String setting)
        {
            object retval = null;

            retval = ConfigurationManager.AppSettings[setting];

            return retval;

        }

        public static bool IsSettingConfigured(String setting)
        {
            return (SettingsUtility.ReadValue(setting) != null);
        }

        public static string ReadStringValue(String setting)
        {
            string retval = String.Empty;

            if (SettingsUtility.IsSettingConfigured(setting))
            {
                retval = SettingsUtility.ReadValue(setting).ToString();
            }

            return retval;
        }

        public static bool ReadBoolValue(String setting)
        {
            bool retval = false;

            if (SettingsUtility.IsSettingConfigured(setting))
            {

                bool evalBool = false;

                bool.TryParse(SettingsUtility.ReadStringValue(setting), out evalBool);

                retval = evalBool;

            }

            return retval;
        }

        public static Int32 ReadInt32Value(String setting)
        {
            Int32 retval = Int32.MinValue;

            if (SettingsUtility.IsSettingConfigured(setting))
            {

                Int32 evalInt32 = Int32.MinValue;

                Int32.TryParse(SettingsUtility.ReadStringValue(setting), out evalInt32);

                if (evalInt32 != Int32.MinValue)
                {
                    retval = evalInt32;
                }
            }

            return retval;
        }

        public static String ReadConfiguredConnectionString()
        {
            String retval = String.Empty;

            if (!IsSettingConfigured("active.connection.string"))
            {
                throw new ConnectionStringConfigurationException("You must configure the 'active.connection.string' setting to identify the active connections");
            }
            else
            {
                if(ConfigurationManager.ConnectionStrings.Count == 0)
                {
                    throw new ConnectionStringConfigurationException("You must configure at least one connection and set it as the active connection with 'active.connection.string'");                    
                }
                else
                {
                    retval =
                        ConfigurationManager.ConnectionStrings[ReadStringValue("active.connection.string")].
                            ConnectionString;

                    if(String.IsNullOrEmpty(retval))
                    {
                        throw new ConnectionStringConfigurationException(String.Format("Could not find the configured connection string using: {0}", ReadStringValue("active.connection.string")));                                            
                    }
                }
            }

            return retval;
        }

        public static bool IsJavaRuntimeAvailable()
        {
            return (SettingsUtility.GetJavaInstallationPath().Length > 0);
        }

        public static string GetJavaInstallationPath()
        {
            string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
            if (!string.IsNullOrEmpty(environmentPath))
            {
                return environmentPath;
            }

            string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
            using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
            {
                string currentVersion = rk.GetValue("CurrentVersion").ToString();
                using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
                {
                    return key.GetValue("JavaHome").ToString();
                }
            }
        }

        public static bool IsLinux
        {
            get
            {
                int p = (int)Environment.OSVersion.Platform;
                return (p == 4) || (p == 6) || (p == 128);
            }
        }

        public static string GetJavaExecutable()
        {
            String retval = String.Empty;

            if(SettingsUtility.IsLinux)
            {
                retval = "java";
            }
            else
            {
                retval = "java.exe";
            }

            return retval;
        }
    }

}
